<?php

namespace App\Http\Controllers\home;

use App\app;
use App\app_comment;
use App\Http\Controllers\Controller;
use App\User;
use App\user_focus;
use App\user_infos;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ajaxController extends Controller
{
    function getCommentCount(Request $request)
    {
        $startDate = $request->input("startDate");
        $endDate = date("Y-m-d", strtotime($request->input("endDate")) + 3600 * 24);
        $Aid = $request->input("Aid");
        $count = DB::select("select count(*) as count,LEFT (time,10) as day from app_comments where time >= \"$startDate\" 
          and time <= \"$endDate\" and AID = $Aid group by LEFT (time,10)");
        $return = array();
        $return['day'] = array();
        $return['count'] = array();
        foreach ($count as $v) {
            array_push($return['day'], $v->day);
            array_push($return['count'], $v->count);
        }
        return json_encode($return);
    }

    function getComment(Request $request)
    {
        $startDate = $request->input("startDate");
        $endDate = date("Y-m-d", strtotime($request->input("endDate")) + 3600 * 24);
        $Aid = $request->input("Aid");
        $categoryId = $request->input("categoryId");
        $rating = $request->input("rating");
        $words = $request->input("words");

        $query = app_comment::where([
            ['time', '>=', $startDate],
            ['time', '<=', $endDate]]);
        if ($Aid !== "0") {
            $query->where("Aid", $Aid);
        }
        if ($categoryId !== "0") {
            $query->where("from_id", $categoryId);
        }
        if ($rating !== "0") {
            $query->where("rating", $rating);
        }
        if (!empty($words)) {
            $query->where(function ($query) use ($words) {
                foreach ($words as $word) {
                    $query->orWhere("text", "like", "%" . $word . "%");
                }
            });
        }
        $res = $query->orderBY("time", 'desc')->paginate(COMMENT_PAGE_SIZE);
        return json_encode($res);
    }

    function getHotWords(Request $request)
    {
        $startDate = $request->input("startDate");
        $endDate = $request->input("endDate");
        $Aid = $request->input("Aid");
        $username = $request->session()->get("username");
        $Uid = User::where("user", $username)->first()->UID;
        $hotReturn = array();
        $hotReturn['total'] = 0;
        $data = array();
        $words = DB::table("lables")
            ->join("app_lables", "lables.LID", "=", "app_lables.LID")
            ->where([
                ["creatorType", 1],
                ["Aid",$Aid]]
                )->get();
        foreach ($words as $word) {
            $count = app_comment::where([
                ['time', '>=', $startDate],
                ['time', '<=', $endDate],
                ["AID", $Aid],
                ['text', 'like', '%' . $word->lable . '%']
            ])->count();

            $data[$word->lable] = $count;
        }
        arsort($data);
        $hotReturn['data']['text'] = array();
        $hotReturn['data']['weight'] = array();
        foreach ($data as $k => $v) {
            if ($v !== 0) {
                $hotReturn['total']++;
                array_push($hotReturn['data']['text'], $k);
                array_push($hotReturn['data']['weight'], $v);
            }
        }

        $userData = array();
        $userReturn = array();
        $userReturn['total'] = 0;
        $words = DB::table("lables")
            ->join("app_lables", "lables.LID", "=", "app_lables.LID")
            ->where([
                ["AID", $Aid],
                ["creatorType", 0]],
                ["creator", $Uid])->get();
        foreach ($words as $word) {
            $count = app_comment::where([
                ['time', '>=', $startDate],
                ['time', '<=', $endDate],
                ["AID", $Aid],
                ['text', 'like', '%' . $word->lable . '%']
            ])->count();

            $userData[$word->lable] = $count;
        }
        arsort($userData);

        $userReturn['data']['text'] = array();
        $userReturn['data']['weight'] = array();
        foreach ($userData as $k => $v) {
            if ($v !== 0) {
                $userReturn['total']++;
                array_push($userReturn['data']['text'], $k);
                array_push($userReturn['data']['weight'], $v);
            }
        }
        $return['hot'] = $hotReturn;
        $return['user'] = $userReturn;
        return json_encode($return);
    }

    function getGroupCount(Request $request)
    {
        $username = $request->session()->get("username");
        $Uid = User::where("user", $username)->first()->UID;
        $comment = DB::table('app_comments')
            ->join("group_apps", "app_comments.AID", "group_apps.AID")
            ->join("users", "group_apps.GID", "users.GID")
            ->select(DB::raw('count(*) as value,rating as name'))
            ->where([
                ['rating', '!=', null],
                ["UID", $Uid]
            ])->groupBy('rating')->get();
        foreach ($comment as $c) {
            switch ($c->name) {
                case 1;
                    $c->name = "一星";
                    break;
                case 2;
                    $c->name = "二星";
                    break;
                case 3;
                    $c->name = "三星";
                    break;
                case 4;
                    $c->name = "四星";
                    break;
                case 5;
                    $c->name = "五星";
                    break;
                case 0:
                    $c->value = null;
                    $c->name = null;
            }
        }

        return json_encode($comment);
    }

    function getRankCount(Request $request)
    {
        $startDate = $request->input("startDate");
        $endDate = date("Y-m-d", strtotime($request->input("endDate")) + 3600 * 24);
        if (empty($startDate)) {
            $startDate = 0;
        }
        if (empty($request->input("endDate"))) {
            $endDate = date("Y-m-d", time());
        }
        $Aid = $request->input("Aid");
        $comment = app_comment::select(DB::raw('count(*) as value,rating as name'))->where([
            ['time', '>=', $startDate],
            ['time', '<=', $endDate],
            ['rating', '!=', null],
            ['Aid', $Aid]
        ])->groupBy('rating')->get();
        foreach ($comment as $c) {
            switch ($c->name) {
                case 1;
                    $c->name = "一星";
                    break;
                case 2;
                    $c->name = "二星";
                    break;
                case 3;
                    $c->name = "三星";
                    break;
                case 4;
                    $c->name = "四星";
                    break;
                case 5;
                    $c->name = "五星";
                    break;
                case 0:
                    $c->value = null;
                    $c->name = null;
            }
        }

        return json_encode($comment);
    }

    function getFollow(Request $request)
    {
        $username = $request->input("username");
        $UID = User::where("user", $username)->first()->UID;
        $SpAID = user_infos::where("IID",$UID)->first()->SpAID;
        $follows = user_focus::where("UID", $UID)->get();
        $apps['follows'] = array();
        $apps['total'] = 0;
        $apps['SpAID'] = $SpAID;
        foreach ($follows as $follow) {
            array_push($apps['follows'], app::where("AID", $follow->AID)->first());
            $apps['total']++;
        }
        return json_encode($apps);
    }

    function getFollowMyWords(Request $request)
    {
        $username = $request->session()->get("username");
        $UID = User::where("user", $username)->first()->UID;
        $Aid = $request->get("AID");
        $words = DB::table("lables")
            ->join("app_lables", "lables.LID", "=", "app_lables.LID")
            ->where([
                ["AID", $Aid],
                ["creatorType", 0]],
                ["creator", $UID])->get();
        $return['words'] = array();
        $return['total'] = 0;
        foreach ($words as $word) {
            array_push($return['words'], $word);
            $return['total']++;
        }

        return json_encode($return);
    }

    function deleteFollowMywords(Request $request)
    {
        $AID = $request->input("AID");
        $LID = $request->input("LID");
        $res = DB::table("app_lables")->where([
            ["AID", $AID],
            ["LID", $LID]])->delete();
        if ($res) {
            return "OK";
        }
        return "";
    }

    function deleteMywords(Request $request)
    {
        $LID = $request->input("LID");

        $res = DB::transaction(function () use ($LID) {
            DB::table('app_lables')->where("LID", $LID)->delete();
            DB::table('lables')->where("LID", $LID)->delete();
        });

        return "OK";
    }

    function getMyWords(Request $request)
    {
        $username = $request->session()->get("username");
        $UID = User::where("user", $username)->first()->UID;
        $words = DB::table("lables")
            ->where([
                ["creatorType", 0]],
                ["creator", $UID])->get();
        $return['words'] = array();
        $return['total'] = 0;
        foreach ($words as $word) {
            array_push($return['words'], $word);
            $return['total']++;
        }

        return json_encode($return);
    }

    function addToFollowWords(Request $request)
    {
        $AID = $request->input("AID");
        $LID = $request->input(("LID"));
        $count = DB::table("app_lables")->where([
            ["AID", $AID],
            ["LID", $LID]])->count();
        if ($count) {
            return "exist";
        }
        $res = DB::table("app_lables")->insert([
            "AID" => $AID,
            "LID" => $LID]);
        if ($res) {
            return "OK";
        }
        return "";
    }

    function addToFollowApps(Request $request)
    {
        $username = $request->session()->get("username");
        $UID = User::where("user", $username)->first()->UID;
        $AID = $request->input("AID");
        $count = DB::table("user_focus")->where([
            ["AID", $AID],
            ["UID", $UID]
        ])->count();
        if ($count) {
            return "exist";
        }

        $res = DB::table("user_focus")->insert([
            "AID" => $AID,
            "UID" => $UID
        ]);
        if ($res) {
            return "OK";
        }
        return "";
    }

    function deleteFollowApps(Request $request)
    {
        $username = $request->session()->get("username");
        $UID = User::where("user", $username)->first()->UID;
        $AID = $request->input("AID");
        $res = DB::table("user_focus")->where([
            ["AID", $AID],
            ["UID", $UID]
        ])->delete();
        if ($res) {
            return "OK";
        }
        return "";
    }

    function specialApp(Request $request){
        $username = $request->session()->get("username");
        $UID = User::where("user", $username)->first()->UID;
        $AID = $request->input("AID");
        $request->session()->put('SpAID', $AID);
        $res = user_infos::where("IID",$UID)->first();
        $res->SpAID = $AID;
        $res->save();
        if ($res) {
            return "OK";
        }
        return "";
    }

    function addMyWords(Request $request)
    {
        $username = $request->session()->get("username");
        $UID = User::where("user", $username)->first()->UID;
        $text = $request->input(("text"));

        $count = DB::table("lables")->where([
            ["lable", $text]
        ])->count();
        if ($count) {
            return "exist";
        }

        $res = DB::table("lables")->insert([
            "lable" => $text,
            "creator" => $UID,
            "creatorType" => 0,
            "tasty" => 0
        ]);
        if ($res) {
            return "OK";
        }
        return "";
    }
}
